package com.ruoyi.report_kf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_kf.domain.ReportKfTlsxzsjlbBaDetail;
import com.ruoyi.report_kf.domain.ReportKfTlsybgBaDetail;
import com.ruoyi.report_kf.domain.vo.ReportKfTlsxzsjlbBaDetailVo;
import com.ruoyi.report_kf.domain.vo.ReportKfTlsybgBaDetailVo;
import com.ruoyi.report_kf.mapper.ReportKfTlsxzsjlbBaDetailMapper;
import com.ruoyi.report_kf.mapper.ReportKfTlsybgBaDetailMapper;
import com.ruoyi.report_tg.domain.bo.TgsybgBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_kf.domain.bo.ReportKfTlsybgBsInfoBo;
import com.ruoyi.report_kf.domain.vo.ReportKfTlsybgBsInfoVo;
import com.ruoyi.report_kf.domain.ReportKfTlsybgBsInfo;
import com.ruoyi.report_kf.mapper.ReportKfTlsybgBsInfoMapper;
import com.ruoyi.report_kf.service.IReportKfTlsybgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填料试验检测报告Service业务层处理
 *
 * @author zyb
 * @date 2024-11-19
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ReportKfTlsybgBsInfoServiceImpl implements IReportKfTlsybgBsInfoService {

    private final ReportKfTlsybgBsInfoMapper baseMapper;
    private final ReportKfTlsybgBaDetailMapper detailMapper;

    /**
     * 查询填料试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportKfTlsybgBsInfoVo queryById(Long excelId) {
        ReportKfTlsybgBsInfoVo reportKfTlsybgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<ReportKfTlsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<ReportKfTlsybgBaDetail>()
            .eq(ReportKfTlsybgBaDetail::getExcelId, excelId)
            .orderByAsc(ReportKfTlsybgBaDetail::getBaRowNumber));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            reportKfTlsybgBsInfoVo.setBaDetailVos(baDetailVos);
        }
        // 生成报告结论
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(reportKfTlsybgBsInfoVo.getJiancjl()) && !"检测结论：".equals(reportKfTlsybgBsInfoVo.getJiancjl())) {
            return reportKfTlsybgBsInfoVo;
        }
        return jcjl(reportKfTlsybgBsInfoVo);
    }

    /**
     * 查询填料试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportKfTlsybgBsInfoVo> queryPageList(ReportKfTlsybgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportKfTlsybgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportKfTlsybgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填料试验检测报告列表
     */
    @Override
    public List<ReportKfTlsybgBsInfoVo> queryList(ReportKfTlsybgBsInfoBo bo) {
        LambdaQueryWrapper<ReportKfTlsybgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportKfTlsybgBsInfo> buildQueryWrapper(ReportKfTlsybgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportKfTlsybgBsInfo> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getTotalPage() != null, ReportKfTlsybgBsInfo::getTotalPage, bo.getTotalPage());
//        lqw.eq(bo.getPage() != null, ReportKfTlsybgBsInfo::getPage, bo.getPage());
//        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportKfTlsybgBsInfo::getBaogbh, bo.getBaogbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportKfTlsybgBsInfo::getJiancdwmc, bo.getJiancdwmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportKfTlsybgBsInfo::getGongcbwyt, bo.getGongcbwyt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportKfTlsybgBsInfo::getYangpmc, bo.getYangpmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportKfTlsybgBsInfo::getYangpbh, bo.getYangpbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportKfTlsybgBsInfo::getYangpsl, bo.getYangpsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportKfTlsybgBsInfo::getYangpzt, bo.getYangpzt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportKfTlsybgBsInfo::getYangpsj, bo.getYangpsj());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), ReportKfTlsybgBsInfo::getJiancrq, bo.getJiancrq());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportKfTlsybgBsInfo::getShiytjwd, bo.getShiytjwd());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportKfTlsybgBsInfo::getShiytjsd, bo.getShiytjsd());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportKfTlsybgBsInfo::getJiancyj, bo.getJiancyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportKfTlsybgBsInfo::getPandyj, bo.getPandyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportKfTlsybgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), ReportKfTlsybgBsInfo::getQuywz, bo.getQuywz());
//        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), ReportKfTlsybgBsInfo::getWeitrwbh, bo.getWeitrwbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), ReportKfTlsybgBsInfo::getTuycd, bo.getTuycd());
//        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), ReportKfTlsybgBsInfo::getDaibsl, bo.getDaibsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportKfTlsybgBsInfo::getFujsm, bo.getFujsm());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportKfTlsybgBsInfo::getJiancjl, bo.getJiancjl());
//        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportKfTlsybgBsInfo::getShigwtdw, bo.getShigwtdw());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportKfTlsybgBsInfo::getGongcmc, bo.getGongcmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportKfTlsybgBsInfo::getShebIds, bo.getShebIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportKfTlsybgBsInfo::getBgFlag, bo.getBgFlag());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportKfTlsybgBsInfo::getJiancyjIds, bo.getJiancyjIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportKfTlsybgBsInfo::getPandyjIds, bo.getPandyjIds());
//        lqw.eq(bo.getExcelResult() != null, ReportKfTlsybgBsInfo::getExcelResult, bo.getExcelResult());
//        lqw.eq(StringUtils.isNotBlank(bo.getJianclb()), ReportKfTlsybgBsInfo::getJianclb, bo.getJianclb());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsfff()), ReportKfTlsybgBsInfo::getYangpsfff, bo.getYangpsfff());
        return lqw;
    }

    /**
     * 新增填料试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportKfTlsybgBsInfoBo bo) {
        ReportKfTlsybgBsInfo add = BeanUtil.toBean(bo, ReportKfTlsybgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细
            ArrayList<ReportKfTlsybgBaDetail> details = new ArrayList<>(8);
            for (long i = 1; i <= 8L; i++) {
                details.add(new ReportKfTlsybgBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details, 8);
        }
        return flag;
    }

    /**
     * 修改填料试验检测报告
     */
    @Override
    @DealHeh
    public Boolean updateByBo(ReportKfTlsybgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportKfTlsybgBsInfo update = BeanUtil.toBean(bo, ReportKfTlsybgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), ReportKfTlsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(ReportKfTlsybgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (ReportKfTlsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
            return;
        }
        bo.setExcelResult(0);
    }

    @Override
    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportKfTlsybgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填料试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ReportKfTlsybgBsInfoVo jcjl(ReportKfTlsybgBsInfoVo vo) {
        return vo;
    }


}
